from interface.user import *

NAME = ''
PASSWD = ''


def register():
    print("注销")
    while True:
        username = input("Your Name: ")
        userpasswd = input("Your Passwd: ")
        t_userpasswd = input("确认密码: ")
        flag, msg = register_interface(username, userpasswd, t_userpasswd)
        if flag:
            print(msg)
            break
        else:
            print(msg)


def login():
    print("登入")
    while True:
        username = input("Your Name: ")
        userpasswd = input("Your Passwd: ")
        flag, msg = login_interface(username, userpasswd)
        global NAME
        global PASSWD
        NAME = username
        PASSWD = userpasswd
        if flag:
            print(msg)
            break
        else:
            print(msg)


def shopping():
    print("Shopping!")
    global NAME
    if NAME == '':
        print("未登入")
    else:
        shoppingthing_interface()
        commname = input("输入你想购买的商品: ")
        flag, msg = shopping_interface(NAME, commname)
        if flag:
            print(msg)
        else:
            print(msg)


def transmoney():
    print("转账")
    global NAME
    if NAME == '':
        print("未登入")
    else:
        transname = input("输入你转账的人: ")
        flag, msg = transmoney_interface(NAME, transname)
        if flag:
            print(msg)
        else:
            print(msg)


def quit_user():
    global NAME
    if NAME == '':
        print("未登入")
    else:
        NAME = ''
def shopps():
    print("购物信息")
    global NAME
    if NAME == '':
        print("未登入")
    else:
        shopps_interface(NAME)


func_dict = {
    "1": login,
    "2": register,
    "3": shopping,
    "4": transmoney,
    "5": shopps,
    "6": quit_user
}


def run():
    while True:
        print('''
        1:登入
        2:注册
        3:购物
        4:转账
        5:查看购物信息
        6:退出
        ''')

        choice_func = input("请输入你选择的功能")
        if choice_func in func_dict:
            func_dict[choice_func]()
        else:
            print("error")
